package com.imooc.controller;


import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBo;
import com.imooc.service.UserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMoocJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "注册登录",tags = {"注册登录相关接口"})
@RestController
@RequestMapping("/passport")
public class PassportController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户名是否存在
     * @param username
     * @return
     */
    @ApiOperation(value = "查询用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public IMoocJSONResult usernameIsExist(@RequestParam String username){
        //1.判断用户名不能为空
        if(StringUtils.isBlank(username)){
            return IMoocJSONResult.errorMsg("用户名不能为空");
        }
        //2.查询用户名是否存在
        boolean isExist = userService.queryUsernameIsExist(username);
        if(isExist){
            return IMoocJSONResult.errorMsg("用户名已存在");
        }
        //3.没有重复存在，返回成功
        return IMoocJSONResult.ok();
    }

    /**
     * 注册接口
     * @param userBo
     * @return
     */
    @ApiOperation(value = "用户注册",notes = "用户注册",httpMethod = "POST")
    @PostMapping("/regist")
    public IMoocJSONResult regist(@RequestBody UserBo userBo, HttpServletRequest request, HttpServletResponse response){
        String username = userBo.getUsername();
        String password = userBo.getPassword();
        String confimPassword = userBo.getConfirmPassword();
        //0.判断用户名和密码必须不为空
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confimPassword)){
            return IMoocJSONResult.errorMsg("用户名和密码不能为空");
        }
        //1.查询用户名是否存在
        boolean isExist = userService.queryUsernameIsExist(username);
        if(isExist){
            return IMoocJSONResult.errorMsg("用户名已存在");
        }
        //2.密码长度不能少于6位
        if(password.length() < 6){
            return IMoocJSONResult.errorMsg("密码长度不能少于6位");
        }
        //3.判断两次密码是否一致
        if(!password.equals(confimPassword)){
            return IMoocJSONResult.errorMsg("两次密码输入不一致");
        }
        //4.实现注册
        Users user = userService.createUser(userBo);
        user = setNullProperty(user);
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(user),true);
        return IMoocJSONResult.ok(user);
    }

    /**
     * 用户登录
     * @param userBo
     * @return
     */
    @ApiOperation(value = "用户登录",notes = "用户登录",httpMethod = "POST")
    @PostMapping("/login")
    public IMoocJSONResult login(@RequestBody UserBo userBo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = userBo.getUsername();
        String password = userBo.getPassword();
        //0.判断用户名和密码必须不为空
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            return IMoocJSONResult.errorMsg("用户名和密码不能为空");
        }

        //1.查询用户名密码是否匹配
        Users users = userService.queryUserForLogin(username, MD5Utils.getMD5Str(password));
        if(users == null){
            return IMoocJSONResult.errorMsg("用户名或密码错误");
        }
        users = setNullProperty(users);
        CookieUtils.setCookie(request,response,"user", JsonUtils.objectToJson(users),true);
        return IMoocJSONResult.ok(users);
    }

    /**
     * 用户退出登录,清除cookie
     * @param userId
     * @return
     */
    @ApiOperation(value = "退出登录",notes = "退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public IMoocJSONResult logout(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //0.判断用户名和密码必须不为空
        if(StringUtils.isBlank(userId)){
            return IMoocJSONResult.errorMsg("用户id不能为空");
        }
        CookieUtils.deleteCookie(request,response,"user");
        return IMoocJSONResult.ok();
    }

    private Users setNullProperty(Users users){
        users.setPassword(null);
        users.setMobile(null);
        users.setEmail(null);
        users.setCreatedTime(null);
        users.setUpdatedTime(null);
        users.setBirthday(null);
        return users;
    }
}
